import api from "../../api/api"
import Toast from '@vant/weapp/toast/toast'
import Dialog from '@vant/weapp/dialog/dialog'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allCollect: [],
    name: '',
  },
  async searchHandler(e) {
    let name = e.detail.value.name
    this.setData({
      name
    })
    if (name == '') {
      this.getAllCollect()
    }
    const res = await api.search({ name })
    if (res.data.code == "Q001") {
      if (res.data.result.length > 0) {
        this.setData({
          allCollect: res.data.result
        })
      } else {
        this.setData({
          allCollect: []
        })
      }
    }
    console.log('搜索', res)
  },
  async getAllCollect() {
    const res = await api.findAllLike({ tokenString: wx.getStorageSync('token') })
    console.log('收藏列表', res)
    this.setData({
      allCollect: res.data.result
    })
  },

  async del(e) {
    let tokenString = wx.getStorageSync('token')
    let pid = e.currentTarget.dataset.item.pid
    let name = e.currentTarget.dataset.item.name
    const res = await api.noLike({ pid, tokenString })
    Dialog.confirm({
      message: `确定要取消收藏”${name}“吗？`,
    }).then(() => {
      if (res.data.code == 900) {
        console.log('取消收藏', res);
        Toast.success(res.data.msg)
        this.getAllCollect()
      }
    }).catch(() => {
    })
  },
  toDetail(e) {
    let pid = e.currentTarget.dataset.pid
    wx.navigateTo({
      url: '/pages/detail/detail?pid=' + pid
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getAllCollect()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getAllCollect()
  },
})